﻿using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using System.IO;
using QuickOpen.Framework.Repositories;

namespace QuickOpen.Framework.Items
{
    /// <summary>
    /// An implementation of the IFilterableItem interface to describe a physical file in a VS solution.
    /// </summary>
    public class File : IFilterableItem
    {
        /// <summary>
        /// The filename portion of the path except when there is a duplicate filename.
        /// In this case the path from the solution directory forward is shown.
        /// </summary>
        public string DisplayName
        {
            get { return ToString(); }
        }

        /// <summary>
        /// The filename portion of the path only.
        /// </summary>
        public string ShortName
        {
            get { return _fileName; }
        }

        /// <summary>
        /// The full path to the file.
        /// </summary>
        public string LongName
        {
            get { return _fullPath; }
        }

        /// <summary>
        /// The full path to the file.
        /// </summary>
        public string FullPath
        {
            get { return _fullPath; }
        }

        /// <summary>
        /// The filename only portion of the file.
        /// </summary>
        public string FileName
        {
            get { return _fileName; }
        }

        /// <summary>
        /// A bool indicating whether or not there was a duplicate filename found.
        /// </summary>
        public bool IsDup
        {
            get { return _isDup; }
            set { _isDup = value; }
        }

        /// <summary>
        /// The repository this file belongs to.
        /// </summary>
        public FileRepository FileRepository
        {
            get { return _repository; }
            set { _repository = value; }
        }

        /// <summary>
        /// Gets the filename without the extension. Path.GetNameWithoutExtension will not work here as
        /// we want only the items after the first period (.)
        /// </summary>
        public string FileNameWithoutExtension
        {
            get
            {
                if (_fileName.IndexOf(".") != -1)
                    return _fileName.Substring(0, _fileName.IndexOf("."));
                else
                    return _fileName;
            }
        }

        /// <summary>
        /// Opens the file in visual studio.
        /// </summary>
        /// <returns>returns true if the file was opened. False otherwise</returns>
        public bool PerformAction()
        {
            try
            {
                _projectItem.Open(Constants.vsViewKindPrimary).Activate();
                return true;
            }
            catch { }

            return false;
        }

        /// <summary>
        /// Returns a string representation of the file.
        /// </summary>
        /// <returns>Either the fullpath - the solution path if the file is a duplicate or just the filename if it's not.</returns>
        public override string ToString()
        {
            if (IsDup)
            {
                string dirToRemove = Path.GetDirectoryName(Path.GetDirectoryName(_projectItem.ContainingProject.FullName));
                return FullPath.Replace(dirToRemove + "\\", string.Empty);
            }
            else
                return FileName;
        }

        /// <summary>
        /// Create a new instance of a File.
        /// </summary>
        /// <param name="item">The VS IDE ProjectItem representing this file.</param>
        public File(ProjectItem item)
        {
            _projectItem = item;
            _fullPath = item.Properties.Item("Fullpath").Value.ToString();
            _fileName = item.Properties.Item("Filename").Value.ToString();
        }

        internal File() { }

        private ProjectItem _projectItem;
        private string _fullPath;
        private string _fileName;
        private bool _isDup;
        private FileRepository _repository;
    }
}
